<?php
/*
 * 手机客户端店铺-加入购物车接口
 * 2014-03-10
 * 2014-05-07 (已查)
 */
include "inc/public_header.php";

include "func/user.php";				//用户信息处理函数


$reJson = array(); //构建返回值

$goodsId 		= $_REQUEST["goodsId"];		//用户提交内容

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
if(!$uInfoArr){
	echo json_encode(array("success"=>"0","errCode"=>"0","msg"=>"没有获取到用户信息"));
	die;
}
$uid = $uInfoArr["uid"];//用户信息主键

$sql = "select uid from shop_product_gwc where product_id = '".$goodsId."' and uid='".$uid."'";
$db1->query($sql);
$db1->next_record();
if ($uid == $db1->f("uid")){
	echo json_encode(array("success"=>"0","errCode"=>"1","msg"=>"购物车中已经存在"));
	die;
}

$sql = "select quantity,salesvolume from shop_product where product_id = '".$goodsId."'";
$db1->query($sql);
$db1->next_record();
if ($db1->f("quantity") == $db1->f("salesvolume")){
	echo json_encode(array("success"=>"0","errCode"=>"2","msg"=>"此商品库存不足，暂时不能购买"));
	die;
}


$business_type = 0;
if ($uid){
$sql = "SELECT business_type FROM user_base where uid='".$uid."'";
$db->query($sql);
if ($db->next_record()){
	$business_type = $db->f("business_type");
}
}
$sql = "select price,qyprice,shop_id from shop_product where product_id = '".$goodsId."' and shangjia='1'";
$db1->query($sql);
if($db1->next_record()){
	if ($business_type==1 and $db1->f("qyprice") and $db1->f("qyprice")<$db1->f("price")){
	$price	   		= $db1->f("qyprice");
	}else{
	$price	   		= $db1->f("price");
	}
	$shop_id	   	= $db1->f("shop_id");
	//购物车商品添加
	$sql = "insert into shop_product_gwc (uid,product_id,shop_id,price,sum) values('".$uid."','".$goodsId."','".$shop_id."','".$price."','1')";
	$db->query($sql);
	$reJson["success"] = "1";
	echo json_encode($reJson);
}else{
	echo json_encode(array("success"=>"0","errCode"=>"3","msg"=>"此商品已下架，暂时不能购买"));
	die;
}
	
?>
